package com.ymatou.apigateway.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.SqPushSqOrderCustomsClearanceStatusReq;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.SqPushSqOrderCustomsClearanceStatusCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_SqPushSqOrderCustomsClearanceStatus {

	private static OpenapiReq openapiReq;
	
	private static SqPushSqOrderCustomsClearanceStatusReq sqPushSqOrderCustomsClearanceStatusReq;
	private static SqPushSqOrderCustomsClearanceStatusCall sqPushSqOrderCustomsClearanceStatusCall;
	
	private static OpenApiWapper openApiWapper;
	private static OrderShardingWapper orderShardingWapper = new OrderShardingWapper();
	
	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId")); 

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("openapi推送海关清关状态");
		openApiWapper = new OpenApiWapper();
	}

	@Before
	public void caseUp() {

		openapiReq = new OpenapiReq();
		
		sqPushSqOrderCustomsClearanceStatusReq = new SqPushSqOrderCustomsClearanceStatusReq();
		sqPushSqOrderCustomsClearanceStatusCall = new SqPushSqOrderCustomsClearanceStatusCall();
		
		openapiReq.setMethod("sq.customs.clearance.status.push");

	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sqPushSqOrderCustomsClearanceStatusCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verify(Long orderId) {
		
		OrderPo orderPo = orderShardingWapper.selectOrderbyOrderId(orderId);
		
		Logger.verifyEquals(sqPushSqOrderCustomsClearanceStatusReq
				.getCustoms_clearance_status(), orderPo
				.getCustomsClearanceStatus(), "验证CustomsClearanceStatus");
		Logger.verifyEquals(sqPushSqOrderCustomsClearanceStatusReq
				.getCustoms_clearance_status_desc(), orderPo
				.getCustomsClearanceStatusDesc(),
				"验证CustomsClearanceStatusDesc");
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqPushSqOrderCustomsClearanceStatus_001() {
		Logger.start(true, "openapi货头推送海关清关状态，推送成功");
		try {
			
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);

			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);
			
			Thread.sleep(5000);
			
			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			Long sellerId = productBean.getGoodsOwnerId();
			
//			Long orderId = 13133818699L;
//			Long sellerId = 500002490L;

			sqPushSqOrderCustomsClearanceStatusReq.setOrder_id(orderId);
			sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status(1);
			sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status_desc("海关通关中");

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqPushSqOrderCustomsClearanceStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.customs.clearance.status.push",
							app_id);

			sqPushSqOrderCustomsClearanceStatusCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			sqPushSqOrderCustomsClearanceStatusCall.setData(openapiReq);
			sqPushSqOrderCustomsClearanceStatusCall.callService();

			Logger.verifyEquals("0000", sqPushSqOrderCustomsClearanceStatusCall.getCode(), 
					"校验code");
			Logger.verifyEquals("操作成功", sqPushSqOrderCustomsClearanceStatusCall.getMsg(),
					"校验message");

			Logger.verifyNotNull(sqPushSqOrderCustomsClearanceStatusCall.getContent(),
					"校验content");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@SuppressWarnings("static-access")
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SqPushSqOrderCustomsClearanceStatus_002() {
		Logger.start(true, "openapi店主推送海关清关状态，推送失败");
		try {
			
			Long orderId = 18956868699L;
			Long sellerId = 501974283L;

			sqPushSqOrderCustomsClearanceStatusReq.setOrder_id(orderId);
			sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status(1);
			sqPushSqOrderCustomsClearanceStatusReq.setCustoms_clearance_status_desc("海关通关中");

			Application application = openApiWapper
					.selectApplicationBySellerId(sellerId).get(0);

			String app_id = application.getApp_id();

			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);

			openapiReq.setApp_id(app_id);

			String app_secret = application.getApp_secret();

			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(sqPushSqOrderCustomsClearanceStatusReq));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.customs.clearance.status.push",
							app_id);

			sqPushSqOrderCustomsClearanceStatusCall.setUrl(url);

			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));

			sqPushSqOrderCustomsClearanceStatusCall.setData(openapiReq);
			sqPushSqOrderCustomsClearanceStatusCall.callService();

			Logger.verifyEquals("0009", sqPushSqOrderCustomsClearanceStatusCall.getCode(), 
					"校验code");
			Logger.verifyEquals("处理失败,请稍后重试", sqPushSqOrderCustomsClearanceStatusCall.getMsg(),
					"校验message");
			Logger.verifyEquals(null, sqPushSqOrderCustomsClearanceStatusCall.getContent(),
					"校验content");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		
//		Ts_SqPushSqOrderCustomsClearanceStatus test = new Ts_SqPushSqOrderCustomsClearanceStatus();
//		test.Tc_sqgetorderlist_008();
	}
}
